<!--管理员查看定时邮箱列表-->
<style>
	.yanse {
		width: 20px;
		height: 20px;
		display: inline-block;
		line-height: 20px;
	}
	
	.input-group {
		margin-bottom: 20px;
	}
	
	.xiaofangkuai {
		display: inline-block;
	}
</style>

<h3 class="text-center" style="margin-bottom: 20px;">定时邮箱维护</h3>

<table class="table table-striped table-bordered table-hover">
	<tr>
		<th col style="width: 20%">邮件类型</th>
		<th col style="width: 30%">邮件名称</th>
		<th col style="width: 50%">邮件内容</th>
		<th col style="width: 20%">操作</th>
	</tr>
</table>

<!-- 修改邮件模态框 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">修改毕设阶段信息</h4>
			</div>
			<div class="modal-body">
				<div class="input-group">
					<span class="input-group-addon" id="youjianmingcheng1">邮件类型</span>
					<input id="type_mail" type="text" class="form-control" aria-describedby="youjianmingcheng1" disabled="disabled">
				</div>
				<div class="input-group">
					<span class="input-group-addon" id="youjianmingcheng1">邮件名称</span>
					<input id="title_mail" type="text" class="form-control" placeholder="请修改邮件名称" aria-describedby="youjianmingcheng1">
				</div>
				<!-- 这里还要放一个富文本编辑器 -->
				<div class="input-group">
					<span class="input-group-addon" id="basic-addon1">邮件内容</span>
					<script id="editor" type="text/plain" charset="utf-8" style="width: 100%; height: 240px;"></script>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" data-dismiss="modal" id="ok_change">确定修改</button>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript" charset="utf-8">
	//实例化编辑器
	UE.delEditor('editor');
	//建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
	var ue = UE.getEditor('editor', {
		initialFrameWidth: '100%', //设置编辑器宽度
		scaleEnabled: true, //设置不自动调整高度
		//scaleEnabled {Boolean} [默认值：false]//是否可以拉伸长高，(设置true开启时，自动长高失效)
		toolbars: [
			['toggletool', 'fullscreen', 'source', '|', 'undo', 'redo', '|',
				'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript',
				'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
				'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', '|',
				'simpleupload', 'insertimage', '|', 'selectall', 'cleardoc'
			]
		]
	});
</script>

<script>
	var myajax = $.ajax({
		xhrFields: {
			withCredentials: true
		},
		type: 'get',
		url: getFullURL('admin/mail-list'),
		data: {},
		dataType: 'json',
		success: function(data) {
			console.log(data)
			if(data.status == 0) {
				$('.table').html("");

				var str = `
					<thead><tr>
						<th col style="width: 25%">邮件类型</th>
						<th col style="width: 30%">邮件名称</th>
						<th col style="width: 50%">邮件内容</th>
						<th col style="width: 15%">操作</th>
					</tr></thead>
				`;
				$('.table').append(str);

				var str_ = `<tbody>`;

				$.each(data.list, function(i, n) {
					var val = '';
					if(n.stage == 1) {
						val = "开题报告模板";
					} else if(n.stage == 2) {
						val = "任务书模板";
					} else if(n.stage == 3) {
						val = "终稿模板";
					} else if(n.stage == 4) {
						val = "定稿模板";
					} else if(n.stage == -1) {
						val = "毕设选报通过模板";
					} else if(n.stage == -2) {
						val = "毕设选报未通过模板";
					}

					var str = `
						<tr>
							<td>` + val + `</td>
							<td>` + n.title + `</td>
							<td>` + n.context + `</td>
							<td>
								<div class="btn-group" role="group" aria-label="...">
									<button type="button" class="btn btn-default change" data-toggle="modal" value="` + n.mid + `">修改</button>
								</div>
							</td>
						</tr>
					`;
					str_ = str_ + str;
				})
				str_ = str_ + `</tbody>`;
				$('.table').append(str_);
			} else {
				console.log('无数据或请求失败')
			}
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	})

	$.when(myajax).done(function() {
		$('.change').click(function() {
			$.session.set("mid", $(this).val());
			$.ajax({
				xhrFields: {
					withCredentials: true
				},
				type: 'get',
				url: getFullURL('admin/select-mail'),
				data: {
					mid: $(this).val()
				},
				dataType: 'json',
				success: function(data) {
					console.log(data)
					if(data.status == 0) {
						var val = '';
						if(data.stage == 1) {
							val = "开题报告模板";
						} else if(data.stage == 2) {
							val = "任务书模板";
						} else if(data.stage == 3) {
							val = "终稿模板";
						} else if(data.stage == 4) {
							val = "定稿模板";
						} else if(data.stage == -1) {
							val = "毕设选报通过模板";
						} else if(data.stage == -2) {
							val = "毕设选报未通过模板";
						}

						$('#type_mail').val(val);
						$('#title_mail').val(data.title);

						ue.ready(function() {
							//设置默认值
							ue.setContent(data.content);
						});

						$('#myModal').modal({
							keyboard: true,
							show: true
						})
					} else {
						console.log('无数据或请求失败')
					}
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
		})
	})

	$('#ok_change').click(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'post',
			url: getFullURL('admin/update-mail'),
			data: {
				mid: $.session.get("mid"),
				title: $('#title_mail').val(),
				content: UE.getEditor('editor').getContent()
			},
			dataType: 'json',
			success: function(data) {
				console.log(data)
				if(data.status == 0) {
					alert("修改成功！");
					setTimeout(function() {
						$('.center').html('');
						$('.center').load('./adminEmail.html');
					}, 300)
				} else {
					console.log('无数据或请求失败')
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})
</script>